package com.woniuxy.aspect;

import com.woniuxy.entity.Game;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.jetbrains.annotations.NotNull;

//自定义切面类
public class AspectJava {
    //前置通知
    public void myBefore(@NotNull JoinPoint joinPoint){//AOP中携带的方法，可以支持获取目标类和方法的参数
        System.out.println("前置通知：模拟事务的开启……");
        System.out.println("获取目标类："+joinPoint.getTarget());
        System.out.print("，被织入增强的目标方法是："+joinPoint.getSignature().getName());
    }
    //后置通知
    public void myAfterReturning(@NotNull JoinPoint joinPoint){
        System.out.println("后置通知：模拟事务的提交……");
        System.out.print("，被织入增强的目标方法是："+joinPoint.getSignature().getName());
    }
    //可以修改入参和返回！
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知开启：");
        //可以获取入参
        Object[] args = proceedingJoinPoint.getArgs();
        args[0] = 1;
        //反射的执行方法invoke意思
        Game game = (Game) proceedingJoinPoint.proceed(args);
        game.setAccount("查数据……");
        return game;
    }
    //异常通知
    public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("抛出异常："+e.getMessage());
    }
    //最终通知
    public void myAfter(){
        System.out.println("最终通知的执行！");
    }
}
